package com.hellpro.lolbox.home.weight;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.home.collections.CollectIfro;
import com.hellpro.lolbox.utils.UIManger;

import butterknife.BindView;


public class HomeZhanJIActivity extends LolBaseActivity {


    @BindView(R.id.activity_home_zhan_ji_name)
    EditText activityHomeZhanJiName;
    @BindView(R.id.activity_home_zhan_ji_qu)
    EditText activityHomeZhanJiQu;
    @BindView(R.id.activity_home_zhan_ji_button)
    Button activityHomeZhanJiButton;
    @BindView(R.id.activity_home_zhan_ji_bar)
    Toolbar activityHomeZhanJiBar;
    @BindView(R.id.activity_home_zhan_ji_mybutton)
    Button activityHomeZhanJiMybutton;
    private PopupWindow popupWindow;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_zhan_ji;
    }


    @Override
    public void initView() {
        Intent intent = getIntent();
        boolean type = intent.getBooleanExtra("type",false);
        if (type){
            activityHomeZhanJiButton.setText("收藏");
        }

        //申请权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] ss = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
            int i = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (i != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(ss, 1);
            }
        }
        activityHomeZhanJiBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityHomeZhanJiMybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(HomeZhanJIActivity.this, CollectIfro.class);
            }
        });


        activityHomeZhanJiQu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(v);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                imm.hideSoftInputFromWindow(activityHomeZhanJiQu.getWindowToken(), 0);

            }
        });
        activityHomeZhanJiButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable text = activityHomeZhanJiName.getText();
                String name = text.toString().trim();
                Editable text1 = activityHomeZhanJiQu.getText();
                String qu = text1.toString().trim();
                if (!name.equals("")&&!qu.equals("")) {
                    Intent intent = new Intent(v.getContext(), ZhanJIChaXunActivity.class);
                    intent.putExtra("name", name);
                    intent.putExtra("qu", qu);
                    startActivity(intent);

                }


            }
        });
    }

    private void showPopupWindow(View view) {
        //一个自定义的布局，作为显示容器
        final String[] names = new String[]{"电信一", "电信二", "电信三", "电信四", "电信五", "电信六", "电信七", "电信八", "电信九", "电信十", "电信十一", "电信十二", "电信十三", "电信十四", "电信十五", "电信十六", "电信十七", "电信十八", "电信十九", "网通一"
                , "网通二", "网通三", "网通四", "网通五", "网通六", "网通七", "教育一"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, names);
        View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
        ListView listView = (ListView) contentView.findViewById(R.id.popwindows_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = names[position];
                activityHomeZhanJiQu.setText(name);
                popupWindow.dismiss();

            }
        });

        //创建pop
        popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        popupWindow.showAsDropDown(view);
    }



}
